auto();

var storage = storages.create("YYS_YGW_VEG");
BEFORE_WAIT = storage.get("BEFORE_WAIT", 270);
VEG_TIME = storage.get("VEG_TIME", 270);
VEG_COUNT = storage.get("VEG_COUNT", 100);
FARM_RATE = storage.get("FARM_RATE", 0.5);
START = false;
LOCK = false;
THERAD = undefined;

const HEIGHT = 2340;
const WIDTH = 1080;
setScreenMetrics(WIDTH, HEIGHT);

var setText = function (text) {
    ui.run(function () {
        winfo.text.setText(text);
    });
};
var winfo = floaty.window(
    <frame gravity="center" textSize="16sp" bg="#ffffffff">
        <text id="text"></text>
    </frame>
);
var winput = floaty.window(
    <frame gravity="center" textSize="8px" top="16sp" bg="#ffffffff">
        <vertical>
            <text>等待时间：</text>
            <input id="initime" focusable="true" inputType="number" text="270" />
            <text>种植时间：</text>
            <input id="time" focusable="true" inputType="number" text="270" />
            <text>施肥概率：</text>
            <input id="farm" focusable="true" inputType="numberDecimal" text="1.0" />
            <text>数量：</text>
            <input id="count" focusable="true" inputType="number" text="100" />
            <button id="ok">UNLOCK</button>
        </vertical>
    </frame>
);
winput.setPosition(winfo.getX(), winfo.getY() + winfo.getHeight());
winfo.exitOnClose();
winput.exitOnClose();
winput.initime.setText(BEFORE_WAIT.toString());
winput.time.setText(VEG_TIME.toString());
winput.farm.setText(FARM_RATE.toString());
winput.count.setText(VEG_COUNT.toString());
winput.ok.setText(LOCK ? "LOCK" : "UNLOCK");
winput.initime.on("touch_down", () => {
    if (LOCK) return;
    winput.requestFocus();
    winput.initime.requestFocus();
});
winput.time.on("touch_down", () => {
    if (LOCK) return;
    winput.requestFocus();
    winput.time.requestFocus();
});
winput.farm.on("touch_down", () => {
    if (LOCK) return;
    winput.requestFocus();
    winput.farm.requestFocus();
});
winput.count.on("touch_down", () => {
    if (LOCK) return;
    winput.requestFocus();
    winput.count.requestFocus();
});
winput.ok.on("click", () => {
    LOCK = !LOCK;
    START = !START;
    winput.ok.setText(LOCK ? "LOCK" : "UNLOCK");

    BEFORE_WAIT = parseInt(winput.initime.text());
    VEG_TIME = parseInt(winput.time.text());
    FARM_RATE = parseFloat(winput.farm.text());
    VEG_COUNT = parseInt(winput.count.text());
    storage.put("BEFORE_WAIT", BEFORE_WAIT);
    storage.put("VEG_TIME", VEG_TIME);
    storage.put("FARM_RATE", FARM_RATE);
    storage.put("VEG_COUNT", VEG_COUNT);
    toast("设置成功");
    winput.disableFocus();
    winput.initime.clearFocus();
    winput.time.clearFocus();
    winput.farm.clearFocus();
    winput.count.clearFocus();
    winput.initime.setFocusableInTouchMode(!LOCK);
    winput.time.setFocusableInTouchMode(!LOCK);
    winput.farm.setFocusableInTouchMode(!LOCK);
    winput.count.setFocusableInTouchMode(!LOCK);
    if (!LOCK && THREAD) {
        THREAD.interrupt();
    }
    else if (LOCK) {
        THREAD = threads.start(main);
    }
});
setInterval(() => { }, 1000);

function main() {
    while (1) {
        while (START && (VEG_COUNT > 0)) {
            if (BEFORE_WAIT > 0) {
                for (BEFORE_WAIT; BEFORE_WAIT > 0; BEFORE_WAIT--) {
                    sleep(1000);
                    setText(BEFORE_WAIT + "秒后开始执行");
                }
            }

            setText("尝试启动应用");
            var lunch_ok = app.launch("com.netease.yokaikoya")
            if (lunch_ok) {
                setText("启动应用 成功");
            }
            else {
                setText("启动应用 失败， 退出");
                exit(1);
            }
            sleep(rand_high(1000));
            setText("请勿操作");
            sleep(rand_high(2000));
            count = 0;
            if (VEG_COUNT >= 12)
                count = 12;
            else
                count = VEG_COUNT;
            // no safe
            VEG_COUNT -= count;
            storage.put("VEG_COUNT", VEG_COUNT);
            winput.count.setText(VEG_COUNT.toString());

            sleep(rand_high(2000));
            p1(count);
            sleep(rand_high(500));
            p2(count);

            waitForSleep(VEG_TIME, VEG_COUNT);
        }

        sleep(rand_high(5000));
        setText("执行完成！");
    }
}

function waitForSleep(time, left) {
    for (var i = time; i > 0; i--) {
        sleep(1000);
        setText(i + "秒后开始下一次执行, 剩余约" + left);
    }
};

function rand(number, percent) {
    var dia = Math.floor(Math.random() * (number * percent));
    var result = number + ((Math.random() > 0.5) ? dia : -dia);
    log(number, '==>', result);
    return result;
}
function rand_low_low(number) {
    return rand(number, 0.002);
}
function rand_low(number) {
    return rand(number, 0.02);
}
function rand_high(number) {
    return rand(number, 0.1);
}

function bezier_curves(cp, t) {
    cx = 3.0 * (cp[1].x - cp[0].x);
    bx = 3.0 * (cp[2].x - cp[1].x) - cx;
    ax = cp[3].x - cp[0].x - cx - bx;
    cy = 3.0 * (cp[1].y - cp[0].y);
    by = 3.0 * (cp[2].y - cp[1].y) - cy;
    ay = cp[3].y - cp[0].y - cy - by;

    tSquared = t * t;
    tCubed = tSquared * t;
    result = {
        "x": 0,
        "y": 0
    }
    result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
    result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
    return result;
}

function sml_swipe(qx, qy, zx, zy, time) {
    var xxy = [time];
    var point = [];

    var dx0 = {
        "x": qx,
        "y": qy
    }
    var dx1 = {
        "x": random(qx - 100, qx + 100),
        "y": random(qy, qy + 50)
    }
    var dx2 = {
        "x": random(zx - 100, zx + 100),
        "y": random(zy, zy + 50),
    }
    var dx3 = {
        "x": zx,
        "y": zy
    }

    for (var i = 0; i < 4; i++) {
        eval("point.push(dx" + i + ")");
    }

    for (let i = 0; i < 1; i += 0.08) {
        xxyy = [parseInt(bezier_curves(point, i).x), parseInt(bezier_curves(point, i).y)]

        xxy.push(xxyy);

    }

    gesture.apply(null, xxy);
}

function mySwipe(step, right, length, dua) {
    var start = (WIDTH - length) / 2;
    var end = WIDTH - start;
    return function () {
        for (var i = 0; i < step; i++) {
            if (right) {
                sml_swipe(end, rand_low(500), start, rand_low(500), dua);
            }
            else {
                sml_swipe(start, rand_low(500), end, rand_low(500), dua);
            }
        }
    }
}

function farmGet(op, count, mode) {
    function farmCell(x, y) {
        click(rand_low(x), rand_low_low(y));
        click(rand_low(x), rand_low_low(y));
        click(rand_low(x), rand_low_low(y));
        sleep(rand_high(500));
        op();
    }
    click(rand_low(610), rand_low_low(880));
    sleep(rand_high(500));
    if (count >= 1)
        farmCell(550, 880);
    if (count >= 2)
        farmCell(550, 1000);
    if (count >= 3)
        farmCell(550, 1120);
    if (count >= 4)
        farmCell(800, 880);
    if (count >= 5)
        farmCell(800, 1000);
    if (count >= 6)
        farmCell(800, 1120);

    // pool farm
    if (mode == 0) {
        farmCell(800, 1460);
        farmCell(800, 1590);
        farmCell(800, 1740);
        farmCell(1000, 1460);
        farmCell(1000, 1590);
        farmCell(1000, 1740);
    }
}

function toEdge() {
    setText("边界对齐");
    mySwipe(14, true, rand_low(800), rand_high(200))();
    sleep(rand_high(100));
};
function toFarm(step, pos) {
    setText("移动到目标位置");
    mySwipe(step, pos, rand_low(330), rand_high(1000))();
    sleep(rand_high(100));
};
function toNextFarm(step, pos) {
    setText("移动到目标位置");
    mySwipe(step, pos, rand_low(360), rand_high(1000))();
    sleep(rand_high(100));
};

function p1(count) {
    var getVeg = function () {
        setText("收菜");
        click(rand_low(540), rand_low(2240));
        sleep(rand_high(500));
        click(rand_low(540), rand_low(2240));
        sleep(rand_high(500));
        click(rand_low(540), rand_low(2240));
        sleep(rand_high(500));
        click(rand_low(540), rand_low(2240));
        sleep(rand_high(1000));
    };

    toEdge();
    toFarm(8, false);
    farmGet(getVeg, count, 0);

    toNextFarm(1, true);
    farmGet(getVeg, count - 6, 1);
}

function p2(count) {
    var setVeg = function () {
        setText("种菜");
        click(rand_low(100), rand_low(2100));
        sleep(rand_high(500));
    };
    var waterVeg = function () {
        setText("浇水");
        click(rand_low(540), rand_low(2240));
        sleep(rand_high(500));
    };
    var fabVeg = function () {
        setText("施肥");
        if (Math.random() > (1 - FARM_RATE)) {
            click(rand_low(250), rand_low(2100));
        }
        sleep(rand_high(500));
    };

    toEdge();
    toFarm(8, false);
    farmGet(setVeg, count, 0);
    farmGet(waterVeg, count, 0);
    farmGet(fabVeg, count, 0);

    toNextFarm(1, true);
    farmGet(setVeg, count - 6, 1);
    farmGet(waterVeg, count - 6, 1);
    farmGet(fabVeg, count - 6, 1);
}